﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Text;
using UnityEngine.UI;

/// <summary>
/// 打印日志
/// </summary>
public class LogSystem : MonoBehaviour
{
    public StringBuilder logs = new StringBuilder();
    public ScrollRect scrollRect;
    public Text text;

    private void Start()
    {
        // LogLine("start");
    }

    private void Update()
    {
        // Debug.Log(Time.time.ToString("f2"));
    }

    /// <summary>
    /// 打印一行日志
    /// </summary>
    public void LogLine(string str)
    {
        string s = FormatTime(Time.time - GameManager.Instance.timeDifference) + " " + str;

        if (logs.Length == 0)
        {
            logs.Append(s);
        }
        else
        {
            logs.Append("\n"+s);
        }
        RefreshText();
    }

    /// <summary>
    /// 打印日志
    /// </summary>
    public void Log(string str)
    {
        logs.Append(str);
        RefreshText();
    }

    private void RefreshText()
    {
        text.text = logs.ToString();
        text.rectTransform.sizeDelta = text.rectTransform.sizeDelta + new Vector2(0.01f, 0.01f);
        scrollRect.verticalScrollbar.value=0.0f;
    }

    private string FormatTime(float sec)
    {
        TimeSpan ts = new TimeSpan(0, 0, Convert.ToInt32(sec));
        string str = "";

        if (ts.Hours > 0)
        {
            str = ts.Hours.ToString("00") + ":" + ts.Minutes.ToString("00") + ":" + ts.Seconds.ToString("00");
        }
        if (ts.Hours == 0 && ts.Minutes > 0)
        {
            str = ts.Minutes.ToString("00") + ":" + ts.Seconds.ToString("00");
        }
        if (ts.Hours == 0 && ts.Minutes == 0)
        {
            str = "00:" + ts.Seconds.ToString("00");
        }
        
        return "<color=blue>"+str+"</color>";
    }
}
